package com.zhhs.galaxyott.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhhs.galaxyott.R;
import com.zhhs.galaxyott.fragment.AboutUsFragment;
import com.zhhs.galaxyott.fragment.CollectRecordFragment;
import com.zhhs.galaxyott.fragment.PersonalFragment;
import com.zhhs.galaxyott.fragment.WatchRecordFragment;
import com.zhhs.galaxyott.sharepreference.SharedPrefHelper;
import com.zhhs.galaxyott.utils.Logs;

public class DeviceCenterActivity extends FragmentActivity {

    private static String TAG = DeviceCenterActivity.class.getSimpleName();

    public static LinearLayout titlelayout;
    private PersonalFragment mPersonalFragment;
    private AboutUsFragment mAboutUsFragment;
    private WatchRecordFragment mWatchRecordFragment;
    private CollectRecordFragment mCollectRecordFragment;
    public static int index = 0;
    private TextView titleText, person_center;

    private SharedPrefHelper sp;
    public static boolean detailFocus = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        sp = SharedPrefHelper.getInstance(this);
        // 去除title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 去掉Activity上面的状态栏.
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        index = getIntent().getIntExtra("number", -1);

        setContentView(R.layout.device_center);

        detailFocus = false;

        titlelayout = (LinearLayout) findViewById(R.id.title_layout);
        getTitle(index).setBackgroundResource(R.drawable.title_focus);

        if (sp.getIsComboOnline()) {
            getTitle(3).setVisibility(View.VISIBLE);
        } else {
            getTitle(3).setVisibility(View.INVISIBLE);
            getTitle(0).setText("观看记录");
            getTitle(1).setText("收藏记录");
            getTitle(2).setText("关于我们");
        }

        mPersonalFragment = (PersonalFragment) getSupportFragmentManager().findFragmentById(R.id.details);

        switchFragment(index);
        titleText = getTitle(index);
        // titleText.setTextColor(getResources().getColor(R.color.text_white));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        return super.dispatchKeyEvent(event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        int count = titlelayout.getChildCount();

        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_CENTER:

            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            if (sp.getIsComboOnline()) {
                if (index < count - 1 && !detailFocus) {
                    // getTitle(index).setTextColor(getResources().getColor(R.color.text_white));
                    getTitle(index).setBackgroundResource(R.drawable.rect_transparent_bg);
                    index++;
                    // getTitle(index).setTextColor(getResources().getColor(R.color.text_white));

                    getTitle(index).setBackgroundResource(R.drawable.title_focus);

                    switchFragment(index);
                    return true;
                } else {
                    break;
                }
            } else {
                if (index < count - 2 && !detailFocus) {
                    getTitle(index).setBackgroundResource(R.drawable.rect_transparent_bg);
                    index++;

                    getTitle(index).setBackgroundResource(R.drawable.title_focus);

                    switchFragment(index);
                    return true;
                } else {
                    break;
                }
            }

        case KeyEvent.KEYCODE_DPAD_UP:
            if (index > 0 && !detailFocus) {
                // getTitle(index).setTextColor(getResources().getColor(R.color.text_white));
                getTitle(index).setBackgroundResource(R.drawable.rect_transparent_bg);
                index--;
                // getTitle(index).setTextColor(getResources().getColor(R.color.text_white));
                getTitle(index).setBackgroundResource(R.drawable.title_focus);

                switchFragment(index);
                return true;
            } else {
                break;
            }

        case KeyEvent.KEYCODE_DPAD_RIGHT:
            if (!detailFocus)
                getTitle(index).setBackgroundResource(R.drawable.title_normal);
            break;
        default:

            break;
        }
        // 防止焦点移动两个位置
        if (switchFragmentKeyDown(index, keyCode, event))
            return true;

        Logs.logD("DEBUG_1", "THIS IS -->" + keyCode);
        if (!detailFocus) {
            getTitle(index).setBackgroundResource(R.drawable.title_focus);
        }
        return super.onKeyDown(keyCode, event);
    }

    private Boolean switchFragmentKeyDown(int index, int keyCode, KeyEvent event) {
        if (sp.getIsComboOnline()) {
            switch (index) {
            case 0:
                PersonalFragment.onKeyDown(keyCode, event);
                break;
            case 1:
                return WatchRecordFragment.onKeyDown(keyCode, event);
            case 2:
                return CollectRecordFragment.onKeyDown(keyCode, event);
                // case 3:
            default:
                break;

            }
        } else {
            switch (index) {
            case 0:
                return WatchRecordFragment.onKeyDown(keyCode, event);
            case 1:
                return CollectRecordFragment.onKeyDown(keyCode, event);
                // case 3:
            default:
                break;

            }
        }

        return false;
    }

    private void switchFragment(int index) {
        if (sp.getIsComboOnline()) {
            switch (index) {
            case 0:
                mPersonalFragment = PersonalFragment.newInstance();
                getSupportFragmentManager().beginTransaction().replace(R.id.details, mPersonalFragment).commit();
                break;
            case 1:
                mWatchRecordFragment = WatchRecordFragment.newInstance();
                getSupportFragmentManager().beginTransaction().replace(R.id.details, mWatchRecordFragment).commit();
                break;
            case 2:
                mCollectRecordFragment = CollectRecordFragment.newInstance();
                getSupportFragmentManager().beginTransaction().replace(R.id.details, mCollectRecordFragment).commit();
                break;
            case 3:
                mAboutUsFragment = AboutUsFragment.newInstance();

                getSupportFragmentManager().beginTransaction().replace(R.id.details, mAboutUsFragment).commit();
                break;
            default:
                break;
            }
        } else {
            switch (index) {
            case 0:
                mWatchRecordFragment = WatchRecordFragment.newInstance();
                getSupportFragmentManager().beginTransaction().replace(R.id.details, mWatchRecordFragment).commit();
                break;
            case 1:
                mCollectRecordFragment = CollectRecordFragment.newInstance();
                getSupportFragmentManager().beginTransaction().replace(R.id.details, mCollectRecordFragment).commit();
                break;
            case 2:
                mAboutUsFragment = AboutUsFragment.newInstance();

                getSupportFragmentManager().beginTransaction().replace(R.id.details, mAboutUsFragment).commit();
                break;
            default:
                break;
            }
        }

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        // if (!detailFocus) {
        // super.onBackPressed();
        // } else
        // Logs.logD("DEBUG_01", "THIS IS ONBACKPRESSED");
    }

    private TextView getTitle(int index) {
        TextView txttitle = (TextView) titlelayout.getChildAt(index);
        return txttitle;
    }

}
